package com.example.gitproject.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.gitproject.Entity.Book;
import com.example.gitproject.Entity.User;
import com.example.gitproject.Entity.vo.LoginVo;
import com.example.gitproject.dao.BookMapper;
import com.example.gitproject.dao.UserMapper;
import com.example.gitproject.service.BookService;
import com.example.gitproject.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public List<User> findAllUser() {
        List<User> list = userMapper.selectList(null);
        return list;
    }


    @Override
    public List<User> getUsersByName(String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();  // 条件查询体    条件筛选   模糊查询

        wrapper.eq(true, "name", name);  // SELECT * FROM user WHERE name = #{qiu}
        wrapper.like("name", name);           // "SELECT * FROM user WHERE name LIKE 't' "
        return baseMapper.selectList(wrapper);
    }


    @Override
    public Boolean login(LoginVo loginVo) {

        //登录成功校验

        //创建条件器对象
        QueryWrapper<User> wrapper = new QueryWrapper<>();

        String name = loginVo.getName();
        String password = loginVo.getPassword();
        //构造条件
        wrapper.eq("name", name).eq("password", password);
        //select *  from  user  where  name = " {#name}"  and  password = "{#password}"
        User user = baseMapper.selectOne(wrapper);    // 1	qiu	18	123456	admin

        //4.判断这个user是否为空    return false
        if (user == null) {
            return false;
        }
        //5. 作比较 比较的结果只会是true  false       通常来讲密码做比较的
        return loginVo.getPassword().equals(user.getPassword());

        //  比较外部传入的 账号密码 是否  等于user的账号密码    name.equals(user.getName());
    }

    @Override
    public User getUserAll(LoginVo loginVo) {

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();

        String name = loginVo.getName();
        String password = loginVo.getPassword();

        //构造条件
        userQueryWrapper.eq("name", name).eq("password", password);      //select *  from  user  where  name = " {#name}"  and  password = "{#password}"


        return baseMapper.selectOne(userQueryWrapper);


    }


}
